package com.ujs.edu.demo.controller;

import com.ujs.edu.demo.config.NeedLogin;
import com.ujs.edu.demo.dao.BcAdminMapper;
import com.ujs.edu.demo.dao.BcBooksMapper;
import com.ujs.edu.demo.dao.BcCartMapper;
import com.ujs.edu.demo.dataobject.BcAdmin;
import com.ujs.edu.demo.dataobject.BcBooks;
import com.ujs.edu.demo.dataobject.BcCart;
import com.ujs.edu.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.List;

@Controller
public class BookFirstController {

    @Autowired
    private BcAdminMapper bcAdminMapper;

    @Autowired
    private BcBooksMapper bcBooksMapper;

    @Autowired
    private BcCartMapper bcCartMapper;



    @NeedLogin
    @RequestMapping(path = "/bookfirst")
    public ModelAndView bookFirst(@SessionAttribute("bcAdmin") BcAdmin bcAdmin1){

        ModelAndView view=new ModelAndView();

        BcAdmin bcAdmin=bcAdminMapper.selectByPrimaryKey(bcAdmin1.getAdminId());
        view.addObject("admin",bcAdmin);

        List<BcCart>bcCarts=bcCartMapper.selectAll();
        view.addObject("carts",bcCarts);


        view.setViewName("bookfirst");
        return view;
    }


    //按类别展示书籍信息
    @RequestMapping(path = "/bookcartcard")
    public ModelAndView bookcartcard(@RequestParam("cartId") Integer cartId) {
        ModelAndView view=new ModelAndView();
        List<BcBooks> bcBooks;
        if (cartId != 0) {
           String cartId1=cartId.toString();
            bcBooks = bcBooksMapper.selectByCartId(cartId1);
        }else {
            bcBooks=bcBooksMapper.selectByScore();
        }

        view.addObject("books",bcBooks);
        view.setViewName("bookshow");
        return view;
    }


    //搜索
    @RequestMapping(path = "bookfirstsearch")
    public ModelAndView bookFirstSearch(@RequestParam("input") String input){
        ModelAndView view=new ModelAndView();

        List<BcBooks>bcBooks=bcBooksMapper.selectLike(input);
        view.addObject("books",bcBooks);

        view.setViewName("bookshow");

        return view;
    }
}
